using System;
using Sce.PlayStation.Core;
using Sce.PlayStation.Core.Graphics;
namespace Nyanimals
{
	public class CStaticContext : CBaseContext
	{
        public Texture2D debugTexOverride = null;
        
		public CStaticContext ()
		{
		}		
		public override ContextTypes Type
		{
			get { return ContextTypes.Static_Context;}
		}
		public override void Render(GraphicsContext graphics) 
		{
			// leave in not active
			if(Active == false)
			{
				return;
			}
			
			// get our shader program
			tShader shader;
			CShaderManager.Instance.GetShader(ShaderIndex, out shader);
			
			// get our mesh
			CMesh mesh;
			CMeshManager.Instance.GetMesh(MeshIndex, out mesh);
			
			// get the texture(s)       
			tTexture tex;
            int t = mesh.TextureList.Count;
            if(debugTexOverride!= null)
            { 
                tex = new tTexture();
                tex.Texture = debugTexOverride;
            }
            else if(t == 0)
            {
			    CTextureManager.Instance.GetTexture(0, out tex);
            }
            else
            {
                CTextureManager.Instance.GetTexture(mesh.TextureList[0], out tex);
            }
			
			// calculate the world view projection
			CCamera cam = CCamera.Instance;
			Matrix4 wvp =  cam.Projection * cam.View * WorldMatrix;
			
			
			// set uniform
			int wvpUniform = CContextManager.Instance.GetStaticUniform("WorldViewProj");
			shader.Shader.SetUniformValue(wvpUniform, ref wvp);
			
			// draw
			graphics.SetShaderProgram(shader.Shader);
            graphics.SetTexture(0, tex.Texture);    
       		graphics.SetVertexBuffer(0, mesh.VertBuffer);
			
			if(WireFrame || true)
			{
				graphics.SetLineWidth(0.02f);
				graphics.DrawArrays(DrawMode.Lines, 0, mesh.NumIndices);
			}
			else
			{
        		graphics.DrawArrays(DrawMode.Triangles, 0, mesh.NumIndices);
			}
		}
	}
}

